package pkg.drowne.org;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class login extends Activity implements OnClickListener, Runnable {

	// Views
	private Button loginButton, resetButton;
	private EditText editUsername, editPassword;
	private boolean connected;
	private String serverAddress = "www.drowne.com";
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
	        WindowManager.LayoutParams.FLAG_FULLSCREEN,
	        WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        // creates the activity
        setContentView(R.layout.login);
        
        // variables initialization
        connected = false;
        
        // Views
        registerViews();
        setupOnClickListeners();
    }
    
    private void registerViews() {
		
    	loginButton = (Button) this.findViewById(R.id.btnLogin);
    	resetButton = (Button) this.findViewById(R.id.btnReset);
    	editUsername = (EditText) this.findViewById(R.id.edtUsername);
    	editPassword = (EditText) this.findViewById(R.id.edtPassword);
	}

	public void setupOnClickListeners() {
    	
        loginButton.setOnClickListener(this);
        resetButton.setOnClickListener(this);
    	
    }

	public void onClick(View v) {
		
		switch(v.getId()) {
			
		case R.id.btnLogin:
			// login button pressed
			// validate the data locally
            if (!connected) {
            	Thread cThread = new Thread(this);
            	cThread.start();
            }			
			break;
			
		case R.id.btnReset:
			// reset button pressed
			// so reset each text field
			editUsername.setText("");
			editPassword.setText("");
			break;
		
		
		}
		
	}

	public void run() {

		try {
			InetAddress serverAddr = InetAddress.getByName(serverAddress);
			Log.d("CLIENT", "Connecting...");
			Socket socket = new Socket(serverAddr, 80);
			connected = true;
			while (connected) {
				try {
					Log.d("CLIENT", "Sending command.");
					PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
					out.println("Hey Server!");
					Log.d("CLIENT", "Sent");
					connected = false;
				} catch (Exception e) {
					Log.e("CLIENT", "Error", e);
					connected = false;
				}
			}
			socket.close();
			Log.d("CLIENT", "Closed.");
			connected = false;
		} catch (Exception e) {
			Log.e("CLIENT", "Error", e);
			connected = false;
		}


	}
	
	
}
